package net.caixiaomi.info.ui.football;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.ElementCellModel;
import net.caixiaomi.info.model.MatchPlaysList;
import net.caixiaomi.info.model.PlayFootballItem;
import net.caixiaomi.info.util.Constants;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 2选1
 */

public class TakeOrderTowInOneProxy extends TakeOrderItemViewProxy<MultiItemEntity, BaseViewHolder> {
    private FootballTakeOrderAdapter mAdapter;

    public TakeOrderTowInOneProxy(FootballTakeOrderAdapter adapter) {
        super(adapter);
        mAdapter = adapter;
    }

    @Override
    public void bindView(BaseViewHolder helper, MultiItemEntity item) {
        try {
            PlayFootballItem model = (PlayFootballItem) item;
            StringBuilder stringBuilder = new StringBuilder();
            helper.setGone(R.id.btn_bold, true);
            helper.setGone(R.id.btn_delete, true);

            if (!TextUtils.isEmpty(model.getHomeTeamRank())) {
                stringBuilder.append("[").append(model.getHomeTeamRank()).append("]");
            }
            stringBuilder.append(model.getHomeTeamAbbr());
            helper.setText(R.id.host, stringBuilder);

            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(model.getVisitingTeamRank())) {
                stringBuilder.append("[").append(model.getVisitingTeamRank()).append("]");
            }
            stringBuilder.append(model.getVisitingTeamAbbr());
            helper.setText(R.id.guest, stringBuilder);


            MatchPlaysList matchPlaysList = model.getMatchPlays().get(0);

            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String time = sdf.format(new Date(Long.parseLong(model.getBetEndTime()) * 1000));
            stringBuilder.append(model.getLeagueAddr()).append(" ").append(model.getChangci()).append(" ").append(CommonApp.getInstance().getString(R.string.C_END_TIME)).append(time);
            helper.setText(R.id.text, stringBuilder);
            stringBuilder.setLength(0);



            ElementCellModel homeCell = matchPlaysList.getHomeCell();
            homeCell.setPlayType(matchPlaysList.getPlayType());
            stringBuilder.append(homeCell.getCellName()).append(homeCell.getCellOdds());

            helper.setText(R.id.btn_host, stringBuilder);
            helper.getView(R.id.btn_host).setSelected(homeCell.isSelected());

            helper.setTag(R.id.btn_host, R.id.item_model, model);
            helper.setTag(R.id.btn_host, R.id.item_cell, homeCell);

            helper.setTextColor(R.id.btn_host, ContextCompat.getColor(CommonApp.getInstance(), homeCell.isSelected() ? R.color.white : R.color.primary_text));
            helper.setOnClickListener(R.id.btn_host, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                    PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                    if (mAdapter.getListener() != null) {
                        if (mAdapter.getListener().canChange(tagModel)) {
                            tag.setSelected(!tag.isSelected());
                            if (tagModel.getSelectCell() == null) {
                                tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                            }
                            if (tag.isSelected()) {
                                tagModel.getSelectCell().add(tag);
                            } else {
                                tagModel.getSelectCell().remove(tag);
                            }
                            mAdapter.getListener().onItemChangeListener(tagModel);
                        }
                    }
                }
            });

            stringBuilder.setLength(0);

            ElementCellModel visitingCell = matchPlaysList.getVisitingCell();
            visitingCell.setPlayType(matchPlaysList.getPlayType());
            stringBuilder.append(visitingCell.getCellName()).append(visitingCell.getCellOdds());
            helper.setText(R.id.btn_guest, stringBuilder);
            helper.setTextColor(R.id.btn_guest, ContextCompat.getColor(CommonApp.getInstance(), visitingCell.isSelected() ? R.color.white : R.color.primary_text));
            helper.getView(R.id.btn_guest).setSelected(visitingCell.isSelected());

            helper.setTag(R.id.btn_guest, R.id.item_model, model);
            helper.setTag(R.id.btn_guest, R.id.item_cell, visitingCell);
            helper.setOnClickListener(R.id.btn_guest, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                    PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                    if (mAdapter.getListener() != null) {
                        if (mAdapter.getListener().canChange(tagModel)) {
                            tag.setSelected(!tag.isSelected());
                            if (tagModel.getSelectCell() == null) {
                                tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                            }
                            if (tag.isSelected()) {
                                tagModel.getSelectCell().add(tag);
                            } else {
                                tagModel.getSelectCell().remove(tag);
                            }
                            mAdapter.getListener().onItemChangeListener(tagModel);
                        }
                    }
                }
            });

            helper.addOnClickListener(R.id.btn_delete);

            helper.addOnClickListener(R.id.btn_bold);
            helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            boolean hasSelected = homeCell.isSelected() || visitingCell.isSelected();
            if (hasSelected) {
                helper.getView(R.id.btn_bold).setEnabled(true);
                if (model.isBlod()) {
                    helper.getView(R.id.btn_bold).setSelected(true);
                    helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary));
                } else {
                    helper.getView(R.id.btn_bold).setSelected(false);
                    if (mAdapter.getMaxDan() > 0) {
                        helper.getView(R.id.btn_bold).setEnabled(true);
                    } else {
                        helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.light_gray));
                        helper.getView(R.id.btn_bold).setEnabled(false);
                    }
                }
            } else {
                helper.getView(R.id.btn_bold).setEnabled(false);
                helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.light_gray));
            }

            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) helper.itemView.getLayoutParams();
            layoutParams.topMargin = (int) CommonApp.getInstance().getResources().getDimension(R.dimen.padding_8);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void addItemType() {
        mAdapter.addItemType(Constants.ITEM_VIEW_ITEM, R.layout.item_take_order_two_in_one_proxy);
    }
}
